package com.thinkgem.jeesite.cn.yy.action;

import com.thinkgem.jeesite.cn.yy.entity.ColAction;
import com.thinkgem.jeesite.cn.yy.entity.ProTableCol;
import com.thinkgem.jeesite.cn.yy.service.ColRecordService;
import com.thinkgem.jeesite.cn.yy.service.ProColAdminService;
import com.thinkgem.jeesite.cn.yy.utils.Paging;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 数据表字段信息Action
 * Created by Administrator on 2017/11/22 0022.
 */
@Controller
@RequestMapping("ProColAdmin")
public class ProColAdminAction {


    @Autowired
    private ProColAdminService proColAdminService;

    @Autowired
    private ColRecordService colRecordService;

    /**
     * 保存数据表字段信息
     * @param procol
     * @param response
     */
    @RequestMapping("save")
    public void save(ProTableCol procol, HttpServletResponse response) throws IOException {
        if(procol.getCol_restrict() != null || procol.getCol_name() != null || procol.getCol_type() != null
                ){

            proColAdminService.save(procol);
            int pid = proColAdminService.getProIdByTid(procol.getTab_id());
            ColAction col = new ColAction("insert into",pid,procol.getTab_id(),procol.getCol_cid());
            colRecordService.save(col);
            response.getWriter().println("{\"message\":\"ok\"}");
            return;
        }
        response.getWriter().println("{\"message\":\"添加失败\"}");
    }


    /**
     * 获取数据表信息
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("getProCols")
    public void getProCols(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int count = proColAdminService.proColCount();

        if(count == 0){
            response.getWriter().println("{\"message\":\"没有数据表信息(没有权限)!\"}");
            return;
        }

        int first=Integer.parseInt(request.getSession().getServletContext().getAttribute("pageSize").toString());
        Paging paging = new Paging(0,first,count);
        List<ProTableCol> proTableColList = proColAdminService.getProCols(paging);

        response.getWriter().println(JSONArray.fromObject(proTableColList).toString());
        response.getWriter().println(JSONArray.fromObject(paging).toString());

        response.getWriter().println("{\"message\":\"ok\"}");

    }

    /**
     * 通过上一页下一页获取信息
     * @param currentPage
     * @param request
     * @param response
     */
    @RequestMapping("getgetProColsPage")
    public void getgetProColsPage(int currentPage,HttpServletRequest request,HttpServletResponse response) throws IOException {
        Paging paging = (Paging) request.getSession().getAttribute("paging");
        paging.setFirstPage((currentPage-1)*paging.getLastPage());
        paging.setCountPage(currentPage);
        List<ProTableCol> proTableColList = proColAdminService.getProCols(paging);

        response.getWriter().println(JSONArray.fromObject(proTableColList).toString());
        response.getWriter().println(JSONArray.fromObject(paging).toString());
        response.getWriter().println("{\"message\":\"ok\"}");
    }

    /**
     * 通过数据表id获取数据表中的所有字段信息
     * @param request
     * @param response
     */
    @RequestMapping("getProColByTid")
    public void getProColByTid(int tab_id,HttpServletRequest request,HttpServletResponse response) throws IOException {
        int count = proColAdminService.getcountByTid(tab_id);
        int first=Integer.parseInt(request.getSession().getServletContext().getAttribute("pageSize").toString());
        Paging paging = new Paging(tab_id,0,first,count);
        List<ProTableCol> proTableColList = proColAdminService.getgetProColByTid(paging);

        response.getWriter().println(JSONArray.fromObject(proTableColList).toString());
        response.getWriter().println(JSONArray.fromObject(paging).toString());

        response.getWriter().println("{\"message\":\"ok\"}");
    }

    /**
     *  通过数据表id获取数据表中的所有字段信息
     *  点击上一页,下一页
     * @param currentPage
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("getProColByTidPage")
    public void getProColByTidPage(int currentPage,HttpServletRequest request,HttpServletResponse response) throws Exception {
        Paging paging = (Paging) request.getSession().getAttribute("paging");
        paging.setFirstPage((currentPage-1)*paging.getLastPage());
        paging.setCountPage(currentPage);

        List<ProTableCol> proTableColList = proColAdminService.getgetProColByTid(paging);

        response.getWriter().println(JSONArray.fromObject(proTableColList).toString());
        response.getWriter().println(JSONArray.fromObject(paging).toString());

        response.getWriter().println("{\"message\":\"ok\"}");
    }

    /**
     * 更新数据表字段信息
     * @param protcl
     * @param response
     * @throws IOException
     */
    @RequestMapping("updateProColByid")
    public void updateProColByid(ProTableCol protcl,HttpServletResponse response) throws IOException {
        proColAdminService.updateProColByid(protcl);
        int pid = proColAdminService.getProIdByTid(protcl.getTab_id());
        ColAction col = new ColAction("Update",pid,protcl.getTab_id(),protcl.getCol_cid());
        colRecordService.save(col);
        response.getWriter().println("{\"message\":\"ok\"}");
    }

    /**
     * 通过Col_id删除数据表字段信息
     * @param response
     */
    @RequestMapping("delProCol")
    public void delProCol(ProTableCol protcl,HttpServletResponse response) throws IOException {
        proColAdminService.delProCol(protcl.getCol_id());
        int pid = proColAdminService.getProIdByTid(protcl.getTab_id());
        ColAction col = new ColAction("delete",pid,protcl.getTab_id(),protcl.getCol_cid());
        colRecordService.save(col);
        response.getWriter().println("{\"message\":\"ok\"}");
    }

}

